<?php 
define('XENON_VERSION', '2.2.1');

class Xenon {
    
    
}

function root_url($relativeUrl = '') {
    $https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
    return
        ($https ? 'https://' : 'http://').
        (!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
        (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
        ($https && $_SERVER['SERVER_PORT'] === 443 ||
        $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
            (($relativeUrl === null)? $_SERVER['REQUEST_URI'] : 
        substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/')).
        ($relativeUrl==null? '' : str_replace('//', '/', '/' . $relativeUrl)));
}

function request_url() {
    return root_url(null);
}

define('ROOT_URL', root_url());
define('AJAX',((!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')));
define('PJAX',(!empty($_SERVER['HTTP_X_PJAX'])));

function __($text, $replace=null) {
    if (!empty(i18n::$defaultInstance)) {
        return i18n::$defaultInstance->__($text, $replace);
    } else {
        if (is_array($text)) {
            foreach ($text as $text) {
                if (!empty($text)) break;
            }
        }
        if ($replace !== null) {
            if (is_array($replace)) {
                foreach ($replace as $key => $str) {
                    $text = preg_replace("#%".preg_quote($key,"#")."%#", $str, $text);
                }
            } else {
                $text = preg_replace("#%.+%#U", $replace, $text);
            }
        }
        return $text;
    }
}

function include_existing() {
    foreach (func_get_args() as $file) {
        if ($file && is_file($file)) {
            include $file;
            return true;
        }
    }
    return false;
}

function getCallingScript($backtraceFromN=-1) {
    $backtraceFromN = abs($backtraceFromN);
    $bt = debug_backtrace();
    if (is_array($bt) && count($bt) > 0) {
        if ($backtraceFromN <= count($bt)) {
            return $bt[$backtraceFromN]['file'];
        } else {
            return $bt[0]['file'];
        }
    } else {
        return __FILE__;
    }
}

/*
 * import(file, file, ...)
 * 
 * Imports multiple files and returns the contents, it does not output anything
 * It will only import each file Once
 * Supports importing all files from specific directory (non-recursive), ex: import('js/*.js', 'css/*.css'); 
 *      // Exemple above will import all .js files in the js directory and all the .css files in the css directory
 * For relative paths, the file path will be relative to the file calling the import function. 
 * For absolute paths, if the directory does not exist on the server, it will be relative path from the document root instead
 */
function import($files_=null) {
    $relative_dir = realpath(dirname(getCallingScript()));
    ob_start();
    foreach (func_get_args() as $fileName) {
        $file = preg_match("#^/#", $fileName)? $fileName : $relative_dir.'/'.$fileName;
        $dirName = realpath(dirname($file));
        if (!is_dir($dirName)) {
            $file = DOCUMENT_ROOT . $file;
            $dirName = realpath(dirname($file));
        }
        if (preg_match("#(^.*)/(\*(\.(([a-z]{1,5})|\*))?)$#i", $file, $matches)) {
            $dir = $matches[1];
            $ext = $matches[2];
            if (is_dir($dir)) {
                if ($ext == '*') $ext = '*.*';
                $files = glob($dir . "/{".$ext."}" , GLOB_BRACE);
            }
            if (empty($files)) $files = array();
        } else {
            $files = array($file);
        }
        foreach ($files as $file) {
            if (is_file($file)) {
                echo "\n";
                include_once $file;
                echo "\n";
            }
        }
    }
    return ob_get_clean();
}

/*
 * array_strip(array, keys) 
 * Strips an array to return only specific keys. 
 * Will keep all 'keys' inside the array and remove the rest. 
 * If first value of 'keys' is FALSE, keep everything except other 'keys'
 * keys can be given as separated arguments
 * 
 * Exemple : 
 * 
 * $array = array('A' => 'foo', 'B' => 'bar', 'C' => 'foobar');
 *  
 *  To return only specified keys : 
 *      array_strip($array, array('A','B')); 
 *      array_strip($array, 'A', 'B'); 
 *      //Resulting : array('A' => 'foo', 'B' => 'bar');
 * 
 *  To return everything except specified keys : 
 *      array_strip($array, array(false,'C')); 
 *      array_strip($array, false, 'C'); 
 *      //Resulting : array('A' => 'foo', 'B' => 'bar');
 * 
 */
function array_strip(array &$array, $keys_=array()) {
    if (is_array($keys_)) {
        $keys = $keys_;
    } else {
        $keys = func_get_args();
        array_shift($keys);
    }
    if (!empty($keys)) {
        foreach ($array as $key=>$val) {
            if ($keys[0] === false XOR !isset($keys[$key])) {
                unset($array[$key]);
            }
        }
    }
    return $array;
}

